<?php
define('SASS_APP_KEY', 'taoguan');
define('SAAS_SECRE_KEY', '49F4589687E79D815339B13A73E5FBB4');

define("SERVICE_IDENT", '');//tg.taoex.com

define("SERVER_TT_HOST","192.168.10.41");
define("SERVER_TT_PORT","15001");


function fetchHostByDomain($server_name) {

    require_once(dirname(__FILE__) . "/saasapi.php");

    $api = new SaasOpenClient();
    $api->appkey = SASS_APP_KEY;
    $api->secretKey = SAAS_SECRE_KEY;
    $api->format = 'json';

    $params = array('server_name' => $server_name);

    $result = $api->execute('host.getinfo_byservername', $params);
	unset($api);
	if ($result->success=='true') {

		return $result->data; 
	} else {
		//Not exists this host in my SAAS
		//die("Sorry , $server_name not in my SAAS.");
		return;
	}
}


function init_domain($server_name){

	$server_name = strtolower($server_name);
	if(function_exists("memcache_connect")){

		$tt_obj = memcache_connect(SERVER_TT_HOST, SERVER_TT_PORT);
	} else {

		$tt_obj = false;
	}

	if ($tt_obj) {

		$preFix = md5(md5(sprintf('%s_%s', $server_name, SERVICE_IDENT)));
		$data = unserialize(memcache_get($tt_obj, $preFix));
		if (empty($data) || $data['STATUS'] <> 'HOST_STATUS_ACTIVE' || $data['END_TIME'] < time()) {

			$saas = fetchHostByDomain($server_name);
			if (is_object($saas)) {

				$data= array(
					'HOST_ID'=> $saas->host_id,
					'DB_USER'=> $saas->db_user,
					'DB_PASSWORD'=>$saas->db_passwd,
					'DB_NAME'=>$saas->db_name,
					'DB_HOST'=>$saas->db_host.":".$saas->db_port,
					'STORE_KEY'=>md5($server_name),
					'NICK_NAME'=>$saas->db_user,
					'STATUS'=>$saas->status, 
					'END_TIME'=>strtotime($saas->cycle_end),
				);
				unset($saas);
			}
		}
	}

	//检查状态及过期时间
	if (empty($data)) {
		//没有开通	
		die('没有开通');
	} elseif ($data['STATUS'] <> 'HOST_STATUS_ACTIVE' && !defined('COMMAND_MODE')) {
		//已经开通，还没有激活
		die('已经开通，系统正在初始化中…… ');
	} elseif (time() > ($data['END_TIME']+86400)  && !defined('COMMAND_MODE')) {
		//已经过期	
		die('已经过期');
	} else {
		//状态正常
		if (defined('COMMAND_MODE')) {
			
			define('DB_REAL_USER', $data['DB_USER']);
			define('DB_REAL_PASSWORD', $data['DB_PASSWORD']);
		}
		$tt_obj->set($preFix, serialize($data));
		foreach($data as $key => $value) {
			
			define($key, $value);
		}
	}
}

init_domain($_SERVER['SERVER_NAME']);

define('LOG_TYPE', 3);

define('FILE_STORAGER','ttsystem');
define('STORAGE_MEMCACHED','192.168.10.41:15002');
define('STORAGE_HOST','');//http://img.tg.taoex.com:80

define('KVSTORE_STORAGE', 'base_kvstore_tokyotyrant');
define('KVSTORE_MEMCACHE_CONFIG', '192.168.10.41:15002');

define('CACHE_STORAGE', 'base_cache_memcache');
define('CACHE_MEMCACHE_CONFIG', '192.168.10.41:15001');

define('KV_PREFIX', md5($_SERVER['SERVER_NAME']));

define('WITHOUT_DESKTOP_APPMGR',true);

function set_certificate($certificate){
    base_kvstore::instance('certificate')->store('cert', $certificate);
}

function get_certificate(){
    if(base_kvstore::instance('certificate')->fetch('cert', $certificate)){
        return $certificate;
    }
}
/**
function get_node_id($app_id) {
	if ($app_id=='ome')
		$app_id = 'omestart';
	if(base_kvstore::instance('certificate')->fetch('nodeid' . $app_id, $node_id)){

		print_r($node_id);
        return unserialize($node_id);
    }
}

function delete_node_id($app_id) {

	//if ($app_id=='ome')
		$app_id = 'omestart';
	
	base_kvstore::instance('certificate')->delete('nodeid' . $app_id);
}

function set_node_id($node_id, $app_id) {

	//if ($app_id=='ome')
		$app_id = 'omestart';

	base_kvstore::instance('certificate')->store('nodeid' . $app_id,  serialize($node_id));
}*/